﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CourseUtils;
using System.IO;
using System.Globalization;

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace CRUD
{
    class Services
    {
        #region static variables
     //   static String Path = @"Table";
        static String StaffFileName = "Staff.txt";
        static String BranchFileName = "Branch.txt";
        String Path2 = ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
        #endregion

        static void Main(string[] args)
        {
            string Path = System.Configuration.ConfigurationManager.AppSettings["k1"];  //<------------
            // create the tools instance
            Directory tools = new Directory();

            #region Create Directory for Data Base
            if (!(tools.IsDirectoryExist(Path)))
            {
                tools.CreateDirectory(Path);
            }
            #endregion

            #region Create File for Data Base "StaffFileName" Table
            if (!(tools.IsTableExist(Path + "\\" + StaffFileName)))
            {
                Console.WriteLine("File .\\{0} does not exist!", Path + "\\" + StaffFileName);
                tools.CreateDataBaseFile(Path + "\\" + StaffFileName);
            }
            else 
                Console.WriteLine("File .\\{0} exists!", Path + "\\" + StaffFileName);
            #endregion

            #region Create File for Data Base "BranchFileName" Table
            if (!(tools.IsTableExist(Path + "\\" + BranchFileName)))
            {
                Console.WriteLine("File .\\{0} does not exist!", Path + "\\" + BranchFileName);
                tools.CreateDataBaseFile(Path + "\\" + BranchFileName);
                Console.WriteLine("And now file .\\{0} exists!", Path + "\\" + BranchFileName);

            }
            else
                Console.WriteLine("File .\\{0} exists!", Path + "\\" + BranchFileName);
            #endregion

            #region Create record in Staff table (there is a bug of datetime field yet)
            ConsoleKeyInfo input;
            Console.WriteLine("If you want to insert record in the Staff table press <Enter>, otherwise press <Esc>");
            input = Console.ReadKey(true);
            while (input.Key != ConsoleKey.Escape)
            {
                if (input.Key != ConsoleKey.Enter)
                {
                    Console.WriteLine("press <Enter> or <Esc> ");
                }
                else
                {
                    Staff nextRecord = new Staff();
                    nextRecord = tools.FeedStaffRecord(nextRecord); // ?
                    String PrepaiedRecord = nextRecord.StaffNo + ";" + nextRecord.FName + ";" + nextRecord.LName + ";" + nextRecord.Position + ";" +
                        nextRecord.Sex + ";" + nextRecord.DateOfBirth.ToString("d", CultureInfo.CreateSpecificCulture("en-US")) + ";" + nextRecord.Salary + ";" + nextRecord.BranchNo + ";";
                    File.AppendAllText(Path + "\\" + StaffFileName, PrepaiedRecord + "\r\n");
                    Console.WriteLine("If you want to continue press <Enter>, otherwise press <Esc>");
                }
                input = Console.ReadKey(true);
            }
                #endregion

            #region Create one record in Branch table (there is a bug of datetime field yet)
            Console.WriteLine("If you want to insert record in the Branch table press <Enter>, otherwise press <Esc>");
            input = Console.ReadKey(true);           
            while (input.Key != ConsoleKey.Escape)
                {
                    if (input.Key != ConsoleKey.Enter)
                    {
                        Console.WriteLine("press <Enter> or <Esc> ");
                    }
                    else
                    {
                        Branch nextBranchRecord = new Branch();
                        nextBranchRecord = tools.FeedBranchRecord(nextBranchRecord);
                        String PrepaiedBranchRecord = nextBranchRecord.branchNo + ";" + nextBranchRecord.street + ";" + nextBranchRecord.city + ";" + nextBranchRecord.postcode + ";";
                        File.AppendAllText(Path + "\\" + BranchFileName, PrepaiedBranchRecord + "\r\n");
                        Console.WriteLine("If you want to continue press <Enter>, otherwise press <Esc>");
                    }
                    input = Console.ReadKey(true);
                }
            #endregion
        }
    }
}
